import re
import hashlib
from random import choice

user_file="database/users"
########################
# Users modifications  #
########################
def searchUser(username):
	file=open(user_file,'r')
	for line in file:
		temp=line.split(";")
		user=temp[0]
		password=temp[1]
		if username == user:
			answer=password.strip()
			break
		else:
			answer="invalide"
	return answer

def addUser(username,password):
	save=[]
	file=open(user_file,'a')
	file.write(username+";"+hashlib.sha224(password.encode('utf-8')).hexdigest()+";user")
	return username+" a ete ajoute aux utilisateurs"

def updateUser(username,password,newPassword):
	save=[]
	file=open(user_file,'r')
	oldPassword=searchUser(username)
	if oldPassword==hashlib.sha224(password.encode('utf-8')).hexdigest():
		for line in file:
			save.append(line.strip())
		file=open(user_file,'w')
		for i in range(len(save)):
			temp=save[i].split(";")
			if temp[0] == username:
				save[i]=username+";"+hashlib.sha224(password.encode('utf-8')).hexdigest()+temp[2]
		for i in range(len(save)):
			file.write(save[i]+"\n")
		answer="mot de passe mis a jour pour "+username
	else:
		answer="erreur, les mots de passes ne correspondent pas"
	return answer

def removeUser(username):
	save=[]
	file=open(user_file,'r')
	for line in file:
		save.append(line.strip())
	file=open(user_file,'w')
	for i in range(len(save)):
		temp=save[i].split(";")
		if temp[0] == username:
			del save[i]
	for i in range(len(save)):
		file.write(save[i]+"\n")
	return "utilisateur "+username+" supprime"

def chercherAction(phrase):
        action=""
        commande=""
        reponse=""
        fileDb=open("database/actions-fr","r")
        nbmclefold=0
        for line in fileDb:
            z=0
            if line[:1] != "#" or line != "\n":
                lcas=line.split(";")
                lmots=lcas[0].split("/")
                nbmclef=len(lmots)
                for i in range(len(lmots)):
                    temp=lmots[i].split("|")
                    for j in range(len(temp)):
                        if temp[j]=="?":
                            temp[j]="\""+temp[j]
                        if re.search(temp[j],phrase,re.IGNORECASE):
                            z+=1
                 
            if z==nbmclef and nbmclef > nbmclefold:
                action=lcas[1]
                commande=lcas[2]
                reponses=lcas[3].split("/")
                reponse=choice(reponses)
                nbmclefold=nbmclef
        return action,commande,reponse.strip()

def chercherPhrase(phrase):
        type=""
        reponse="je ne sais pas"
        filesDb=["actions-fr","base-fr","informatique-fr","politesse-fr","simple-fr"]
        for file in filesDb:
            fichier=open("database/"+file,"r")
            for line in fichier:
                z=0
                if line[:1] != "#" or line != "\n":
                    lcas=line.split(";")
                    lmots=lcas[0].split("/")
                    nbmclef=len(lmots)
                    for i in range(len(lmots)):
                        temp=lmots[i].split("|")
                        for j in range(len(temp)):
                            if temp[j]=="?":
                                temp[j]="\""+temp[j]
                            if re.search(temp[j],phrase,re.IGNORECASE):
                                z+=1
                    if z==nbmclef:
                        try:
                            reponses=lcas[1].split("/")
                            reponse=choice(reponses)
                        except:
                           reponse=lcas[1]
                        if file == "actions-fr":
                            type="action"
        return type,reponse.strip()
